package com.winit.cloudlink.mgmt.api;

import com.winit.cloudlink.mgmt.recovery.MessageBackupTaskManager;
import com.winit.cloudlink.mgmt.recovery.MessageRestoreTaskManager;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by stvli on 2017/3/31.
 */
@RestController
@Api(value = "messages", tags = {"messages"}, description = "云链消息管理接口")
@RequestMapping("/api/messages/")
public class MessageApi {
    private static Logger logger = LoggerFactory.getLogger(MessageApi.class);
    @Autowired
    private MessageBackupTaskManager messageBackupTaskManager;
    @Autowired
    private MessageRestoreTaskManager messageRestoreTaskManager;


    @RequestMapping(value = "/startBackup", method = RequestMethod.POST)
    @ApiOperation(value = "备份消息")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "消息备份文件", response = String.class)})
    public ResponseEntity<String> startBackup(@ApiParam(name = "queueName", value = "队列名", required = true) @RequestParam(value = "queueName", required = true) String queueName,
                                              UriComponentsBuilder ucBuilder, HttpServletRequest request, HttpServletResponse response) {
        String backupFile = messageBackupTaskManager.startBackupTask(queueName);
        return new ResponseEntity<String>(backupFile, HttpStatus.OK);
    }

    @RequestMapping(value = "/stopBackup", method = RequestMethod.POST)
    @ApiOperation(value = "停止备份消息")
    public ResponseEntity<String> stopBackup(@ApiParam(name = "queueName", value = "队列名", required = true) @RequestParam(value = "queueName", required = true) String queueName,
                                             UriComponentsBuilder ucBuilder, HttpServletRequest request, HttpServletResponse response) {
        messageBackupTaskManager.stopBackupTask(queueName);
        return new ResponseEntity<String>(HttpStatus.OK);
    }

    @RequestMapping(value = "/restore", method = RequestMethod.POST)
    @ApiOperation(value = "恢复消息")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "消息恢复数量", response = Long.class)})
    public ResponseEntity<Long> restore(@ApiParam(name = "messageFile", value = "消息文件", required = true) @RequestParam(value = "messageFile", required = true) String messageFile,
                                        UriComponentsBuilder ucBuilder, HttpServletRequest request, HttpServletResponse response) {
        long restoreMessages = messageRestoreTaskManager.restoreMessages(messageFile);
        return new ResponseEntity<Long>(restoreMessages, HttpStatus.OK);
    }
}
